//引入express模块
const express=require("express");
//引入连接池模块
const pool=require("../pool.js")
//创建购物车路由器对象
const r=express.Router();
//购物车添加
r.post("/shopping_reg",(req,res)=>{
	var $cid=req.body.cid
	var $user_id=req.body.user_id
	var $product_id=req.body.product_id
	var $count=req.body.count
		//console.log($count);
	var sql="insert into vip_shopping_cart(cid,user_id,product_id,count) values(?,?,?,?)"
	pool.query(sql,[$cid,$user_id,$product_id,$count],(err,result)=>{
		if(err) throw err;
		//console.log(err)
		if(result.affectedRows>0){
			res.send("1")
		}else{
			res.send("0")
		}
	})
})
r.get("/shopping_reg/check/:cid",(req,res)=>{
	var $cid=req.params.cid;
	var sql="select * from vip_shopping_cart where cid=?"
	pool.query(sql,[$cid],(err,result)=>{
		if(err){
			throw err
		}
		if(result.length>0){
			res.send("1")
		}else{
			res.send("0")
		}
	})
	
})
//购物车搜索
r.get("/shopping_search/:cid",(req,res)=>{
	var $cid=req.params.cid;
	var sql="select * from vip_shopping_cart where cid=?"
	pool.query(sql,[$cid],(err,result)=>{
		if(err){
			throw err
		}
		if(result.length>0){
			res.send(result)
		}else{
			res.send("0")
		}
	})
	
})

//购物车修改
r.put("/shopping_revise",(req,res)=>{
	var obj=req.body;
	var sql="update vip_shopping_cart set? where cid=?"
	pool.query(sql,[obj,obj.cid],(err,result)=>{
		if(err) throw err;
		if(result.affectedRows>0){
			res.send("1")
		}else{
			res.send("0")
		}
	})
})

//购物车删除
r.delete("/shopping_del/:cid",(req,res)=>{
	var $cid=req.params.cid;
	var sql="delete from vip_shopping_cart where cid=?"
	pool.query(sql,[$cid],(err,result)=>{
		if(err) throw err;
		if(result.affectedRows>0){
			res.send("1")
		}else{
			res.send("0")
		}
	})
})

//导出购物车路由器对象
module.exports=r;